clear all


global replica_dir="<add path to your working directory>"
global data_dir="$replica_dir/data"
global output_dir="$replica_dir/output"

global simce_data_dir="<add path to your SIMCE data directory>"
*------------------------------------------------------------------------------*

* SIMCE 2011 4b

*------------------------------------------------------------------------------*

**# Student scores

/*

Note:
-----
mrun: 		unique student identifier to link data across years	
idalumno: 	unique identifier for linkage within year, e.g. parents, teachers surveys
	
------------------------------------------------------------------------------*/


use "$simce_data_dir/4B/simce4b2007/Archivos DTA (Stata)/simce4b2007_alu_publica_final.dta", clear

keep if noptje_lect4b_alu==0 | noptje_mate4b_alu==0 | noptje_nat4b_alu==0 // con puntaje

duplicates report mrun
duplicates examples mrun
duplicates tag mrun, g(ndupobs)
unique mrun

keep if ndupobs==0

rename  (ptje_lect4b_alu 	ptje_mate4b_alu 	ptje_nat4b_alu ) /// 
		(lect_pre			mate_pre			nat_pre)


keep idalumno mrun rbd lect_pre mate_pre nat_pre //noptje_mate4b_alu noptje_lect4b_alu noptje_nat4b_alu

save "$data_dir/simce_b4_2007.dta", replace


*------------------------------------------------------------------------------*

**#  Parental survey

*------------------------------------------------------------------------------*

use "$simce_data_dir/4B/simce4b2007/Archivos DTA (Stata)/simce4b2007_cpad_publica_final.dta", clear

duplicates report	idalumno
duplicates tag 		idalumno, g(ndupobs)
//browse if ndupobs>0
keep if ndupobs==0

foreach var of varlist cpad_*{
replace `var'=. if `var'==0 | `var'==99
}


gen 	dad_edu_pre=cpad_p06-1
replace dad_edu_pre=. if inlist(cpad_p06,1,99,0) 

gen 	mom_edu_pre=cpad_p07-1
replace mom_edu_pre=. if inlist(cpad_p07,1,99,0) 

gen 	pad_inc_pre=cpad_p08
replace pad_inc_pre=. if inlist(cpad_p08,99,0) 

keep idalumno dad_edu_pre mom_edu_pre pad_inc_pre



merge 1:1 idalumno using "$data_dir/simce_b4_2007.dta" 
drop _merge

sort mrun

save "$data_dir/simce_b4_2007.dta", replace



*------------------------------------------------------------------------------*

**#  School survey

*------------------------------------------------------------------------------*

use "$simce_data_dir/4B/simce4b2007/Archivos DTA (Stata)/simce4b2007_rbd_publica_final.dta", clear

egen sch_stno_pre = rowmax(nalu_lect4b_rbd nalu_mate4b_rbd nalu_nat4b_rbd)

rename 	(cod_depe1		cod_grupo 		cod_reg_rbd) ///
		(sch_depe1_pre	sch_gse_pre 	cod_reg_rbd_pre)

keep  rbd sch_stno_pre sch_depe1_pre sch_gse_pre cod_reg_rbd_pre

merge 1:m rbd using "$data_dir/simce_b4_2007.dta"

//browse if _merge==1
keep if _merge==3
drop _merge


drop idalumno
rename rbd rbd_pre
sort mrun

save "$data_dir/simce_b4_2007.dta", replace

*------------------------------------------------------------------------------*

